// components/heart/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    stature:{
      type:Number,
    },
    weight:{
      type:Number,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    circumstances:'',
    myweight:0,//体重
    BMInum:0,//bmi值
    userweight:'',//用户体重
    userheith:'',//用户身高
    flag:0,//标准
    sexflag:0,//性别
    RiskL:'低',
    standardList:[{name:'WHO标准',value:'0',checked:true},{name:'亚洲标准',value:'1',},{name:'中国标准',value:'2'}],//标准数组
    sexList:[{name:'男性',value:'0',checked:true,},{name:'女性',value:'1',}],
    positionList:[{name:'南方地区',value:'0',checked:true},{name:'北方地区',value:'1'}],
    positionFlag:true,
    positionnum:0,
    userecharts:{},//用户数据展示
  },

  /**
   * 组件的方法列表
   */
  methods: {
    inputheith(e){
      if((/^[0-9]+$/).test(e.detail.value)){
        this.setData({
          userheith:e.detail.value
        })
        return        
      }
      this.setData({
        userheith:''
      })
    },
    inputwidth(e){
        if((/^[0-9]+$/).test(e.detail.value)){
          this.setData({
            userweight:e.detail.value
          })
          return        
        }
        this.setData({
          userweight:''
        })
    },
    radioChange(e){
      console.log(e.detail.value)
      const {standardList} = this.data;
      let num='0';
      for (let i = 0, len = standardList.length; i < len; ++i) {
          standardList[i].checked=standardList[i].value === e.detail.value
          num=e.detail.value
      }
      if(e.detail.value==2){
        this.setData({
          positionFlag:false
        })
      }else{
        this.setData({
          standardList,
          flag:num,
          positionFlag:true
        })
      }
    },
    sexrChange(e){
      const {sexList} = this.data;
      let num='0';
      for (let i = 0, len = sexList.length; i < len; ++i) {
        sexList[i].checked=sexList[i].value === e.detail.value
        num=e.detail.value
      }
      this.setData({
        sexList,
        sexflag:num
      })
    },
    positionChange(e){
      const {positionList} = this.data;
      let num='0';
      for (let i = 0, len = positionList.length; i < len; ++i) {
        positionList[i].checked=positionList[i].value === e.detail.value
        num=e.detail.value
      }
      this.setData({
        positionList,
        positionnum:num,
      })
    },

    click(){
      // 计算公式为：BMI=体重÷身高²。（体重单位：千克；身高单位：米。）BMI的公式=体重(kg)/身高(m)^2，
      const {flag,userheith,userweight,sexflag} = this.data
      if(userheith&&userweight){
      let BMI=(userweight/Math.pow(parseFloat(userheith/100).toFixed(2),2)).toFixed(2)
          let newweight=null;
        switch(flag){
          case 0:
            // 18.5到23.9之间,也就是在24之内  标准体重可按照世界卫生组织给出的计算方法，方法是：
            if(sexflag==0){
              // 男性：（身高cm－80）×70﹪＝标准体重
                  newweight=(userheith-80)*0.7
               }else{
               // 女性：（身高cm－70）×60﹪＝标准体重
               newweight=(userheith-70)*0.6
               }
           let obj=WHO(BMI)
           this.setData({
             myweight:parseInt(newweight),
             RiskL:obj.RiskL,
             circumstances:obj.name,
             BMInum:BMI
           })   
            break;
        case 1:
             if(sexflag==0){
                  newweight=(userheith-100)*0.9
               }else{
               newweight=(userheith-100)*0.85
               }
           let newobj=Asia(BMI)

           this.setData({
             myweight:parseInt(newweight),
             RiskL:newobj.RiskL,
             circumstances:newobj.name,
             BMInum:BMI
           })   
            break;
        default:
          if(sexflag==0){
            newweight=(userheith-150)*0.6+48
         }else{
          newweight=(userheith-150)*0.6+50
         }
          let newObj=China(BMI)
          this.setData({
            myweight:parseInt(newweight),
            RiskL:newObj.RiskL,
            circumstances:newObj.name,
            BMInum:BMI
          })   
        break
     }
     this.triggerEvent('getdata',{BMInum:this.data.BMInum,flag:this.data.flag})
    }else{
        wx.showToast({
          title: '请输入可靠数据！',
          icon:'error',
          duration: 2000,
          mask:true
        })
      }
    },
  }
})

    // WHO标准
   function WHO(MBI){
    if( MBI<18.5){
      return{name:'偏瘦',RiskL:'低'}
    }else if(MBI<25){
        return {name:'正常',RiskL:'平均水平'}
    }else if(MBI==25){
      return {name:'超重',RiskL:'略增'}
    }else if(MBI<30){
      return {name:'偏胖',RiskL:'增加'}
    }else if(MBI<35){
      return {name:'肥胖',RiskL:'中度增加'}
    }else if(MBI<40){
      return {name:'重度肥胖',RiskL:'严重增加'}
    }else{
      return {name:'极重度肥胖',RiskL:'非常严重增加'}
    }
}
function Asia(BMi) {
  if( BMi<18.5){
    return{name:'偏瘦',RiskL:'低'}
  }else if(MBI<23){
      return {name:'正常',RiskL:'平均水平'}
  }else if(BMi==23){
    return {name:'超重',RiskL:'略增'}
  }else if(MBI<25){
    return {name:'偏胖',RiskL:'增加'}
  }else if(MBI<30){
    return {BMi:'肥胖',RiskL:'中度增加'}
  }else{
    return {name:'重度肥胖',RiskL:'严重增加'}
  }
}

function China(BMI) {
  if(BMI<18.5){
    return{name:'偏瘦',RiskL:'低'}
  }else if(BMI<24){
      return {name:'正常',RiskL:'平均水平'}
  }else if(BMI==24){
    return {name:'超重',RiskL:'略增'}
  }else if(BMI<28){
    return {name:'偏胖',RiskL:'增加'}
  }
  else{
    return {name:'肥胖',RiskL:'中度增加'}
  }
}